// 多语言
import {createI18n} from "vue-i18n";
import store from "@/store";

import enLocale from './en';
import twLocale from './tw';
import usLocale from './us';
import zhLocale from './zh';

const messages = {
    'zh-CN': zhLocale,
    'zh-TW': twLocale,
    'en': enLocale,
    'en-US': usLocale
}

import zhCN from 'ant-design-vue/es/locale/zh_CN';
import zhTW from 'ant-design-vue/es/locale/zh_TW';
import enGB from 'ant-design-vue/es/locale/en_GB';
import enUS from 'ant-design-vue/es/locale/en_US';
const getAntLocale = (locale: string) => {
    let languages: { [key: string]: any} = {
        'zh-CN': zhCN,
        'zh-TW': zhTW,
        'en': enGB,
        'en-US': enUS
    };
    if (languages[locale]) {
        return languages[locale];
    }
    return languages['zh-CN']
}

const getLocale = () => {
    let currentLocale = store.getters['setting/locale'], locales = Object.keys(messages);
    if (currentLocale) {
        for (const locale of locales) {
            if (currentLocale.indexOf(locale) > -1) {
                return currentLocale
            }
        }
    }
    return navigator.language;
}

const i18n = createI18n({
    locale: getLocale(),
    messages,
    fallbackLocale: 'zh-CN'
});

export {
    getLocale,
    i18n,
    getAntLocale
}


